package cc.linker.steplink.crm.merchant.task;

import cc.linker.steplink.crm.commons.lock.RedisLock;
import cc.linker.steplink.crm.merchant.service.FormPcService;
import cc.linker.steplink.crm.merchanth5.pojo.form.ExFormAttrPo;
import cc.linker.steplink.crm.merchanth5.service.form.FormService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

//@Component
public class FormAutoTask {

    @Autowired
    private FormPcService formPcService;

    @Autowired
    private FormService formService;

    /**
     * 每5分钟同步 表单 标签
     */
//    @RedisLock("FormTagsAuto")
//    @Scheduled(cron = "5 0/10 * * * ?")
    public void formauto() {
        // 获取所有的已设置的表单formIds
        List<String> formIds = formPcService.getAllFormIds();
        // 遍历 表单id
        for (String formId: formIds) {
            // 请求表单系统获取最新的表单和 题目数据
            List<ExFormAttrPo> formAttrInfo = this.formAttrInfo(formId);

            // 判断表单是否存在
            if(formAttrInfo==null || formAttrInfo.size()<1){
                // 表单不存在 删除
                formPcService.deleteByFormId(formId);
                continue;
            }
            // 判断 原题目是否还存在表单中
            HashMap<Long,Object> formAttrMap = new HashMap();
            HashMap<Long,Object> formAttrItemMap = new HashMap();
            formAttrInfo.forEach(formAttr->{
                formAttrMap.put(formAttr.getAttrId(),formAttr.getAttrTitle());
                formAttrItemMap.put(formAttr.getAttrId(),JSON.toJSONString(formAttr.getItemList(),SerializerFeature.WriteMapNullValue));
            });

            // 获取本地记录的form_tags 作对比
            List<Map<String,Object>> tagsList = formPcService.getTagsListByFormId(formId);

            for (Map<String,Object> item:tagsList) {
                // 判断表单题目是否存在
                String attrId = item.get("attrId").toString();
                if(!formAttrMap.containsKey(attrId)){
                    formPcService.deleteById(item.get("id").toString());
                    continue;
                }
                List<Map<String,Object>> formAttrItemList = (List<Map<String,Object>>) JSON.parse(formAttrItemMap.get("attrId").toString());
                HashMap<String,Object> AttrItemValueMap = new HashMap();
                // 题目选项转化为Map
                for (Map<String,Object> ItemValue:formAttrItemList){
                    AttrItemValueMap.put(ItemValue.get("itemId").toString(),ItemValue.get("attrItemValue"));
                }
                // 判断题目选项是否存在
                String attritemids = item.get("attritemids").toString();
                String[] attritemidsList= attritemids.split(",");
                List<Map<String,Object>> attritems = new ArrayList<>();
                for (String attritemid:attritemidsList){
                    if(!AttrItemValueMap.containsKey(attritemid)){
                        formPcService.deleteById(item.get("id").toString());
                        continue;
                    }
                    // 拼装 attritems
                    Map<String,Object> items = new HashMap<>();
                    items.put("id",attritemid);
                    items.put("value",AttrItemValueMap.get(attritemid));
                    attritems.add(items);
                }

                // 更新 题目名称和 条件数组
                Map<String,Object> params = new HashMap<>();
                params.put("attrTitle",formAttrMap.get(attrId));
                params.put("attritems",JSON.toJSONString(attritems));
                params.put("id",Integer.valueOf(item.get("id").toString()));
                params.put("orgId",item.get("orgId").toString());
                formPcService.update(params);
            }
        }
    }

    private List<ExFormAttrPo> formAttrInfo(String formId) {
        //
        return formService.getFormAttrListByAppId(formId);
    }
}
